package com.socket.nio.nio.bytebuf;

import com.socket.nio.nio.util.ByteBufPrinter;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.buffer.Unpooled;

/**
 * @author Hexudong
 * @date 2025/4/12
 */
public class ByteBufCreate {
  public static void main(String[] args) {
    /* 1.非池化 */
    // 非池化堆内存 默认是 256
    ByteBuf heapBuffer1 = Unpooled.buffer();
    ByteBuf heapBuffer2 = Unpooled.buffer(257);
    ByteBufPrinter.prettyPrint(heapBuffer1); // 256
    ByteBufPrinter.prettyPrint(heapBuffer2); // 257
    // 非池化直接内存 默认是 256
    ByteBuf directBuffer = Unpooled.directBuffer();
    ByteBuf directBuffer2 = Unpooled.directBuffer(257);
    ByteBufPrinter.prettyPrint(directBuffer); // 256
    ByteBufPrinter.prettyPrint(directBuffer2); // 257

    System.out.println("-----------------------------------------------------------");
    /* 2.池化 */
    // 使用池化分配器（需要Netty依赖）默认 256
    ByteBuf pooledHeapBuffer = PooledByteBufAllocator.DEFAULT.heapBuffer();
    ByteBuf pooledDirectBuffer = PooledByteBufAllocator.DEFAULT.directBuffer();
    ByteBufPrinter.prettyPrint(pooledHeapBuffer);
    ByteBufPrinter.prettyPrint(pooledDirectBuffer);
  }
}
